package m_api

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

const (
	ResultFail = "FAIL"
	ResultOk   = "OK"
)

type FailResponse struct {
	Result string     `json:"result"`
	Error  OamV4Error `json:"error"`
}

type OamV4Detail struct {
	Desc        string `json:"desc"`
	InvalidInfo struct {
		Param  string `json:"param"`
		Reason string `json:"reason"`
	} `json:"invalidParam"`
}

type OamV4Error struct {
	Err    error       `json:"-"`
	Code   int         `json:"code"`
	Msg    string      `json:"message"`
	Detail OamV4Detail `json:"detail"`
}

type SuccessResult struct {
	Result     string      `json:"result"`
	Result_Set interface{} `json:"result_set"`
}

func NewOamV4Error(code int, causeErr error, msg string, reason string) *OamV4Error {
	return &OamV4Error{
		Err:    causeErr,
		Code:   code,
		Msg:    msg,
		Detail: OamV4Detail{Desc: reason},
	}
}

// 成功业务逻辑处理
func Success(c *gin.Context, data interface{}) {
	resp := SuccessResult{
		Result:     ResultOk,
		Result_Set: data,
	}
	c.JSON(http.StatusOK, resp)
}

// 失败的业务逻辑
func Fail(c *gin.Context, code int, reason string) {
	msg := StatusMaps(code)
	data := NewOamV4Error(code, nil, msg, reason)
	c.JSON(http.StatusOK, data)
}
